package com.alibaba.aliper.server.controller.servlet;


import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import com.alibaba.aliper.server.controller.socket.AliperSocket;
import com.alibaba.aliper.server.model.domain.Point;
import com.alibaba.aliper.server.model.impl.AliperServer;
import com.alibaba.aliper.server.model.interfaces.AgentView;
import com.alibaba.aliper.server.model.interfaces.AliperControll;
import com.alibaba.aliper.server.model.interfaces.AliperView;
import com.alibaba.aliper.server.model.interfaces.ChartView;
import com.alibaba.aliper.server.model.interfaces.LineView;
import com.alibaba.aliper.server.interfaces.AliperRemoteControll;
import com.alibaba.aliper.server.interfaces.AliperRemoteView;
import com.caucho.hessian.server.HessianServlet;

/**
 * Servlet implementation class AliperServlet
 */
public class AliperServlet extends HessianServlet  implements AliperRemoteControll,AliperRemoteView{
	
	@Override
	public void init(ServletConfig arg0) throws ServletException {
		super.init(arg0);
		this.aliperSocket.setAliperServer(serverControll);
		Thread thread = new Thread(this.aliperSocket);
		thread.setDaemon(true);
		thread.start();
	}
	
	
	@Override
	public void destroy() {
		try {
			this.aliperSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	AliperSocket aliperSocket = AliperSocket.getAliperSocket();
	AliperControll serverControll = AliperServer.getAliperServer();
	AliperView serverView = AliperServer.getAliperServer();


	@Override
	public void report(String project,String agentId, String chartId, String lineId,
			long time, double data) {
		serverControll.getAgentOnCreate(project,agentId).getChartOnCreate(chartId).report(lineId, time, data);
	}

	@Override
	public void setRange(String project,String agentId, String chartId, String lineId,
			double min, double max) {
		this.serverControll.getAgentOnCreate(project,agentId).getChartOnCreate(chartId).setRante(lineId, min, max);
	}


	@Override
	public Point[] getPointsAfter(String project,String agent, String chart, String line,
			int index) throws InterruptedException {
		Point[] points = null;
		AgentView agentView = this.serverView.getAgent(project,agent);
		if(agentView != null){
			ChartView chartView = agentView.getChart(chart);
			if(chartView != null){
				LineView lineView = chartView.getLine(line);
				Collection<Point> collection = lineView.getPointsAfter(index);
				if(collection != null)
					points = collection.toArray(new Point[collection.size()]);
			}
		}
		return points;
	}


	@Override
	public String[] getAgents(String project) {
		AgentView[] agents = this.serverView.getAgents(project);
		if(agents == null)
			return null;
		String[] ret = new String[agents.length];
		for(int i = 0; i < ret.length; ++i){
			ret[i] = agents[i].getId();
		}
		return ret;
	}


	@Override
	public String[] getCharts(String project,String agent) {
		AgentView agentView = this.serverView.getAgent(project,agent);
		if(agentView != null){
			ChartView[] charts = agentView.getCharts();
			String[] ret = new String[charts.length];
			for(int i = 0; i < ret.length; ++i){
				ret[i] = charts[i].getId();
			}
			return ret;
		}
		return null;
	}


	@Override
	public String[] getLines(String project,String agent, String chart) {
		AgentView agentView = this.serverView.getAgent(project,agent);
		if(agentView != null){
			ChartView chartView = agentView.getChart(chart);
			if(chartView != null){
				LineView[] lines = chartView.getLines();
				String[] ret = new String[lines.length];
				for(int i = 0; i < ret.length; ++i){
					ret[i] = lines[i].getId();
				}
				return ret;
			}
		}
		return null;
	}


	@Override
	public void restart(String project) {
		this.serverControll.restart(project);
	}


	@Override
	public String[] getPojects() {
		return this.serverView.getPojects();
	}

}
